#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Diagnostics;
using System.Text;
using Debug = UnityEngine.Debug;

namespace PandoraEditor
{
    public static class PdrCMDHelper
    {
        static StringBuilder ms_log;
        static StringBuilder ms_error;

        public static bool ProcessCommand(string command, string argument, string workingDir = "", int timeOut = -1)
        {
            ProcessStartInfo start = new ProcessStartInfo(command);
            start.Arguments = argument;
            start.CreateNoWindow = false;
            start.ErrorDialog = true;
            start.UseShellExecute = false;
            start.WorkingDirectory = workingDir;
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            start.RedirectStandardInput = false;
            Process p = new Process();
            p.StartInfo = start;
            p.EnableRaisingEvents = true;
            // p.Exited += (sender, args) => { Debug.Log(sender.ToString()); };
            p.Start();
            p.WaitForExit(timeOut);
            p.Close();
            return true;
        }

        public static void ProcessPython(string argument)
        {
#if UNITY_IPHONE || UNITY_IOS
            var app = string.IsNullOrEmpty(BuildSetting.LocalServerSetting.path_mac_python) ? "python" : BuildSetting.LocalServerSetting.path_mac_python;
#else
            var app = "python";
#endif
            ProcessCommandEx(app, argument);
        }

        public static void ProcessCommandEx(string command, string argument)
        {
            ms_log = new StringBuilder();
            ms_error = new StringBuilder();
            Process process = new Process();
            process.StartInfo.FileName = command;
            process.StartInfo.Arguments = argument;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.OutputDataReceived += OnOutputDataReceived;
            process.ErrorDataReceived += OnErrorDataReceived;
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
            if (ms_error.Length > 0)
            {
                throw new System.Exception($"Process Failed:\n{ms_error}");
            }
            ms_log = null;
            ms_error = null;
        }

        static void OnOutputDataReceived(object sender, DataReceivedEventArgs args)
        {
            if (!string.IsNullOrEmpty(args.Data))
            {
                ms_log.AppendLine(args.Data);
            }
        }

        static void OnErrorDataReceived(object sender, DataReceivedEventArgs args)
        {
            if (!string.IsNullOrEmpty(args.Data))
            {
                ms_error.AppendLine(args.Data);
            }
        }
    }
}